﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;

namespace WPFExcelReport
{
    public partial class UIMVART : UIVAART
    {
        public UIMVART()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;

        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                VAPR = "";
            }
            else
            {
                if (dwMaster["VAANEUDAT"].ToString().Trim() == "0")
                {
                    dwMaster["VAANEUDAT"] = MV.date;
                }
                VAPR = "VA" + dwMaster["VAANEUDAT"].ToString().Substring(2, 6)
                    + "OF"
                    + CURRENCY + dwMaster["VAAKOLL"].ToString() + dwMaster["VAAART"].ToString();
            }
        }
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
        }
        public override void OnFBRTLChg(double value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFBRTL"] = FBRTL;
            base.OnFBRTLChg(value);
        }
        public override void OnFBRTUChg(double value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFBRTU"] = FBRTU;
            base.OnFBRTUChg(value);
        }
        public override void OnFGRQMChg(double value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFGRQM"] = FGRQM;
            base.OnFGRQMChg(value);
        }
        public override void OnFDDIMChg(string value)
        {

            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFDDIM"] = FDDIM;
            base.OnFDDIMChg(value);
        }
       
       
        public override void OnFFDDISChg(double value)
        {

            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFFDDIS"] = FFDDIS;
            base.OnFFDDISChg(value);
        }
        public override void OnFFDDIKChg(double value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFFDDIK"] = FFDDIK;
            base.OnFFDDIKChg(value);
        }
        public override void OnFGRLFDChg(double value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            dwMaster["WAFGRLFD"] = FGRLFD;
            base.OnFGRLFDChg(value);
        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                dwData = TBMain.NewRow();
                dwData["VAAMANDANT"] = app.Mandant;
                dwData["VAAKOLL"] = ((VVAART)MV).KOLL;
                dwData["VAAARTST"] = ((VVAART)MV).ARTST;
                dwData["VAAART"] = ((VVAART)MV).ART.Trim();
                dwData["VAABEZ"] = ((VVAART)MV).DES.Trim();
                dwData["VAADART"] = ((VVAART)MV).FARBE.Trim();
                dwData["VAAPRGR"] = " AND VAAPRGR LIKE '" + PRODGRP.Trim() + "%'";
                dwData["VAAEXCL"] = IMAGEPATH;

                SResult = TMV.QVA06(dwData);
                MergeData(SResult, false);
                IsReady = true; 
                NormalTask = Task.Factory.StartNew(() => LOADWS04(), new CancellationTokenSource().Token, TaskCreationOptions.None, TaskScheduler.FromCurrentSynchronizationContext());

                base.QueryCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void LOADWS04()
        {
            if (!IsComposition)
                return;
            dwData = TBMain.NewRow();
            dwData["VAAMANDANT"] = app.Mandant;
            dwData["VAAKOLL"] = ((VVAART)MV).KOLL;
            dwData["VAAARTST"] = ((VVAART)MV).ARTST;
            dwData["VAAART"] = ((VVAART)MV).ART.Trim();
            dwData["VAABEZ"] = ((VVAART)MV).DES.Trim();
            dwData["VAADART"] = ((VVAART)MV).FARBE.Trim();
            dwData["VAAPRGR"] = " AND VAAPRGR LIKE '" + PRODGRP.Trim() + "%'";
            dwData["VAAEXCL"] = IMAGEPATH;
            SResult = TMV.QVA06WS04(dwData);
            foreach (DataRow row in SResult.Tables[0].Copy().Rows)
            {
                filter = "VAAKOLL='" + row["VAAKOLL"].ToString() + "' AND VAAART='" + row["VAAART"].ToString() + "'";
                rows = TBMain.Select(filter);
                if (rows.Length == 0) continue;
                rows[0]["COMPOSITION"] = row["COMPOSITION"];
            }
        }

        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            try
            {
                TMV.UVAART01(dwMaster);
                base.SaveCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsImageMax = !IsImageMax;
        }

        private void cImage_MouseWheel(object sender, MouseWheelEventArgs e)
        {

        }

        public override void OnAppModeChg()
        {
            switch (app.company)
            {
                case "TST":
                    switch (app.AppMode)
                    {
                        case "TISSUE":
                            IsSelfART = false;
                            break;
                        case "TMODE":
                            IsSelfART = true;
                            break;

                    }
                    break;
            }

            base.OnAppModeChg();
        }
    }
}

